﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.ThuQuy;
using BLL.ThuQuy;
using WindowsForms.TBKN;
using Entities.Admin;
using System.Runtime.InteropServices;
 

namespace WindowsForms.ThuQuy
{
    public partial class FormThemBaoCao : Form
    {

        string manhanvien;
        public static string mabaocao;
        public static bool state = false;
        public FormThemBaoCao()
        {
          
            InitializeComponent();


            LoadComboBox(NhanVien.Objects.Find(x => x.MaBoPhan == "TQ_001").ToList());
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Bạn có muốn thoát khỏi chương trình", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                this.Close();
            }
            else
            {
                Application.Restart();
            }
        }

        private void ResestText()
        {
            this.mtxtMaBaoCao.Text = "";
            this.txtNoiDung.Text = "";
            this.txtSoTienChi.Text = "";
            this.txtSoTienThu.Text = "";
            this.txtTenBaoCao.Text = "";
            this.cbbNguoiLapBaoCao.Text = "";
        }

        private void btnLamLaiBaoCao_Click(object sender, EventArgs e)
        {
            ResetText();
        }

        private void TimBaoCaoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTimKhieuNai frm = new FormTimKhieuNai();
            frm.FormClosed+=new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        private void btnThemBaoCao_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenBaoCao.Text.Trim() != "" && cbbNguoiLapBaoCao.SelectedIndex > 0
                    && txtSoTienChi.Text.Trim() != "" &&
                    txtSoTienThu.Text.Trim() != "" &&
                    QuanLyBaoCao.KiemTraMaBaoCao(mtxtMaBaoCao.Text))
                {
                    BaoCao bc = new BaoCao();
                    bc.MaBaoCao = mtxtMaBaoCao.Text;
                    bc.TenBaoCao = txtTenBaoCao.Text;
                    bc.IdNguoiLap = manhanvien;
                    bc.NgayLap = dtpNgayLap.Value;
                    bc.SoTienChi = float.Parse(txtSoTienChi.Text);
                    bc.SoTienThu = float.Parse(txtSoTienThu.Text);
                    bc.NoiDung = txtNoiDung.Text;

                    if (QuanLyBaoCao.KiemTraMaBaoCao(mtxtMaBaoCao.Text))
                    {

                        if (QuanLyBaoCao.KiemTra(mtxtMaBaoCao.Text, BaoCao.Objects.All().ToList()))
                        {
                            bool kq = BaoCao.Objects.Add(bc);
                            if (kq)
                            {
                                MessageBox.Show("Thêm thành công");
                            }
                        }
                        else
                        {
                            DialogResult rs = MessageBox.Show("Mã báo cáo đã tồn tại, bạn có muốn cập nhật", "Hỏi",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (rs == DialogResult.Yes)
                            {
                                BaoCao bc1 = BaoCao.Objects.GetById(bc.MaBaoCao);
                                BaoCao.Objects.Update(bc);
                            }
                        }

                    }
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đủ dữ liệu");
                }
            }
            catch (Exception p)
            {
                MessageBox.Show(p.Message);
                
            }
    
            
           
         }

        
        private void LoadComboBox(List<NhanVien> list)
        {
            foreach (var item in list)
                cbbNguoiLapBaoCao.Items.Add(item);
            cbbNguoiLapBaoCao.DisplayMember = "TenNhanVien";
            cbbNguoiLapBaoCao.ValueMember = "MaNhanVien";
            cbbNguoiLapBaoCao.Items.Insert(0, "---Nhân viên---");
            cbbNguoiLapBaoCao.SelectedIndex = 0;
           
            
        }

        private void txtSoTienThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                //MessageBox.Show("Chỉ nhập số");
            e.Handled = true;
        }

        private void txtSoTienChi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar)&& !char.IsControl(e.KeyChar))
                //MessageBox.Show("Chỉ nhập số");
            e.Handled = true;
        }


        public void frm_FormClosed(object sender, EventArgs e)
        {

        }


        private void mtxtMaBaoCao_Validated(object sender, EventArgs e)
        {
            MaBaoCaoerrorProvider1.SetError(mtxtMaBaoCao, null);
        }

        private void mtxtMaBaoCao_Validating(object sender, CancelEventArgs e)
        {
            if(!QuanLyBaoCao.KiemTraMaBaoCao(mtxtMaBaoCao.Text))
            {
                MaBaoCaoerrorProvider1.SetError(mtxtMaBaoCao, "Mã báo cáo phải có 7 ký tự (dạng Bc00000)");
                e.Cancel = true;
            }
        }

        private void cbbNguoiLapBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbNguoiLapBaoCao.SelectedIndex > 0)
                manhanvien = (cbbNguoiLapBaoCao.SelectedItem as NhanVien).MaNhanVien;
        }

        private void FormThemBaoCao_Load(object sender, EventArgs e)
        {
            mtxtMaBaoCao.ReadOnly = state;
            if(state)
            {
                LoadControl(BaoCao.Objects.GetById(mabaocao));
            }
        }
        private void LoadControl(BaoCao bc)
        {
            try
            {
                mtxtMaBaoCao.Text = bc.MaBaoCao;
                string tienthu = String.Format("{0:0,0}", bc.SoTienChi);
                string tienchi = String.Format("{0:0,0}", bc.SoTienThu);
                txtTenBaoCao.Text = bc.TenBaoCao;
                txtSoTienChi.Text =tienthu;
                txtSoTienThu.Text =tienchi;
                dtpNgayLap.Value = bc.NgayLap;
                for (int i = 1; i < cbbNguoiLapBaoCao.Items.Count; i++)
                    if ((cbbNguoiLapBaoCao.Items[i] as NhanVien).MaNhanVien == bc.IdNguoiLap)
                        cbbNguoiLapBaoCao.SelectedIndex = i;



                txtNoiDung.Text = bc.NoiDung;
            }
            catch (Exception p)
            {
                MessageBox.Show(p.Message);
                
            }
    

        }
    }
}
